package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.component.Variate;
import com.github.aloxc.plugin.restplus.component.VariateTableModel;
import com.github.aloxc.plugin.restplus.entity.*;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.openapi.ui.Messages;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * 设置保存相关变量
 */
public class VariateSaveListener implements ActionListener {
    private Variate variate;

    public VariateSaveListener(Variate variate) {
        this.variate = variate;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            VariateTableModel model = (VariateTableModel) variate.getVariateTable().getModel();
            Vector<Setting> settingList = new Vector<>();
            for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
                boolean used = (Boolean) model.getValueAt(rowIndex, 0);
                String type = (String) model.getValueAt(rowIndex, 1);
                String key = (String) model.getValueAt(rowIndex, 2);
                String value = (String) model.getValueAt(rowIndex, 3);
                Setting setting = new Setting(key, value, type, used);
                settingList.add(setting);
            }
//                if(settingList.size() == 0)return;
            if (variate.getSettingType() == SettingType.GLOBAL) {
                if (settingList.size() == 0) {
                    return;
                }
                RestConfig config = variate.getProject().getComponent(RestServicesNavigator.class).getConfig();
                config.getGlobalConfig().setTest(variate.getTestTextArea().getText());
                List<Setting> globalConfigList = config.getGlobalConfig().getSettingList();
                globalConfigList.clear();
                globalConfigList.addAll(settingList);
                config.save(variate.getProject());
            } else if (variate.getSettingType() == SettingType.ENVIRONMENT) {
                boolean https = variate.getHttpsCheckBox().isSelected();
                String host = variate.getHostTextField().getText();
                String strPort = variate.getPortTextField().getText();
                String context = variate.getContextTextField().getText();
                int port = 80;
                if (!StringUtils.isNullOrEmpty(strPort)) {
                    port = Integer.parseInt(strPort);
                }
                if (port <= 0 || port > 65535) {
                    Messages.showMessageDialog(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.port.error"),
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.exception"), Consts.Icons.EXCEPTION);
                }
                if (StringUtils.isNullOrEmpty(variate.getNameTextField().getText())) {
                    Messages.showMessageDialog(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.name.nonull"),
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.exception"), Consts.Icons.EXCEPTION);
                    return;
                }
                String envName = variate.getNameTextField().getText();
                RestConfig config = variate.getProject().getComponent(RestServicesNavigator.class).getConfig();
                Map<String, EnvironmentConfig> envMap = config.getEnv();
                EnvironmentConfig envConfig = envMap.get(envName);
                if (envConfig == null) {
                    envConfig = new EnvironmentConfig(envName, false, https, host, port, settingList, context, variate.getTestTextArea().getText());
                    envMap.put(envName, envConfig);
                } else {
                    envConfig.setHttps(https);
                    envConfig.setHost(host);
                    envConfig.setPort(port);
                    envConfig.setContext(context);
                    envConfig.setTest(variate.getTestTextArea().getText());
                }

                envConfig.getSettingList().clear();
                envConfig.getSettingList().addAll(settingList);
                config.save(variate.getProject());
            } else if (variate.getSettingType() == SettingType.REQUEST) {
                RestConfig config = variate.getProject().getComponent(RestServicesNavigator.class).getConfig();
                RequestConfig requestConfig = config.getRequestConfig().get((variate.getParentName() != null ? variate.getParentName() + ":" : "") + variate.getUrl());
                if (requestConfig == null) {
                    requestConfig = new RequestConfig(settingList, variate.getTestTextArea().getText());
                    config.getRequestConfig().put((variate.getParentName() != null ? variate.getParentName() + ":" : "") + variate.getUrl(), requestConfig);
                } else {
                    requestConfig.setTest(variate.getTestTextArea().getText());
                    requestConfig.getSettingList().clear();
                    requestConfig.getSettingList().addAll(settingList);
                }
                config.save(variate.getProject());
            }

        } catch (NumberFormatException nfe) {
            Messages.showMessageDialog(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.port.shouldbenumber"), I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.exception"), Consts.Icons.EXCEPTION);
        }
    }
}
